package com.yingxin.yx.framework.security.user;

import com.yingxin.yx.framework.user.dto.UserDto;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

/**
 * @author fanchao
 */
@Data
@NoArgsConstructor
public class PrincipalDto extends UserDto implements Serializable, UserDetails {


    private static final long serialVersionUID = -8358017011093009055L;

    /**
     * 状态
     */
    private String status;


    /**
     * 显示名称
     */
    private String displayName;


    /**
     * 用户参数
     */
    private Map<String, String> userParamMap;


    /**
     * 角色权限
     */
    private Collection<GrantedAuthority> authorities;

    /**
     * 账户是否过期
     */
    private boolean isAccountNonExpired = false;
    /**
     * 账户是否被锁定
     */
    private boolean isAccountNonLocked = false;
    /**
     * 证书是否过期
     */
    private boolean isCredentialsNonExpired = false;
    /**
     * 账户是否有效
     */
    private boolean isEnabled = true;

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return isAccountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return isAccountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return isCredentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return isEnabled;
    }


    public void buildAuthorities() {
        authorities = new ArrayList<>();
        for (String permissionString : resourceSet) {
            if (StringUtils.isNotEmpty(permissionString)) {
                authorities.add(new SimpleGrantedAuthority(permissionString));
            }
        }
    }


    /**
     * 用户登录不能用id，activiti验证可以用id
     */
    private String findByIdFlag;

    public String getFindByIdFlag() {
        return findByIdFlag;
    }

    public void setFindByIdFlag(String findByIdFlag) {
        this.findByIdFlag = findByIdFlag;
    }

}